package com.shuiyun.jtt.config;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import com.shuiyun.jtt.anno.Mapping;
import com.shuiyun.jtt.anno.Message;
import com.shuiyun.jtt.handler.Handler;
import com.shuiyun.jtt.utils.DeviceCache;
import com.shuiyun.jtt.utils.SessionKey;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Set;

/**
 * @author shuiyun
 * @program simulator
 * @description 缓存配置类
 * @date 2022-04-19 19:46
 **/
public class CacheConfig {

    private static final String MESSAGE_PACKAGE_PATH = "com.shuiyun.jtt.message.jt808";
    private static final String ENDPOINT_PACKAGE_PATH = "com.shuiyun.jtt.endpoint";

    /**
     * 初始化入口
     *
     * @author shuiyun
     * @date 2022-04-19 19:57
     */
    @PostConstruct
    public void init() {
        // 扫描加载所有的message
        initMessage();
        // 扫描加载所有的endpoint
        initEndpoint();
        // 初始化设备参数
        initTerminalParam();
        // 初始化行驶记录仪参数
        initTachographParam();
    }

    /**
     * 初始化行驶记录仪参数
     *
     * @author shuiyun
     * @date 2022/5/11 23:39
     */
    private void initTachographParam() {
        DeviceCache.instance.getTachographParamMap().put(0x00, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x01, "510723199302221234".getBytes(Charset.forName("GBK")));
        DeviceCache.instance.getTachographParamMap().put(0x03, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x04, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x05, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x06, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x07, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x08, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x09, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x10, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x11, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x12, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x13, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x14, new byte[]{0x03, 0x00});
        DeviceCache.instance.getTachographParamMap().put(0x15, new byte[]{0x03, 0x00});
    }

    /**
     * 初始化设备参数
     *
     * @author shuiyun
     * @date 2022/5/10 21:40
     */
    private void initTerminalParam() {
        DeviceCache.instance.getTerminalParamMap().put(0x0001, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x0002, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x0003, 3);
        DeviceCache.instance.getTerminalParamMap().put(0x0004, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x0005, 3);
        DeviceCache.instance.getTerminalParamMap().put(0x0006, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x0007, 3);

        DeviceCache.instance.getTerminalParamMap().put(0x0010, "mnc.mcc.gprs");
        DeviceCache.instance.getTerminalParamMap().put(0x0011, "shuiyun");
        DeviceCache.instance.getTerminalParamMap().put(0x0012, "shuiyun123");
        DeviceCache.instance.getTerminalParamMap().put(0x0013, "192.168.100.101");
        DeviceCache.instance.getTerminalParamMap().put(0x0014, "mnd.mcc.gprs");
        DeviceCache.instance.getTerminalParamMap().put(0x0015, "shuiyun");
        DeviceCache.instance.getTerminalParamMap().put(0x0016, "shuiyun456");
        DeviceCache.instance.getTerminalParamMap().put(0x0017, "192.168.100.102");

        DeviceCache.instance.getTerminalParamMap().put(0x001A, "www.shuiyun.com");
        DeviceCache.instance.getTerminalParamMap().put(0x001B, "8023");
        DeviceCache.instance.getTerminalParamMap().put(0x001C, "18023");
        DeviceCache.instance.getTerminalParamMap().put(0x001D, "spare.shuiyun.com");

        DeviceCache.instance.getTerminalParamMap().put(0x0020, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0021, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0022, 30);
        DeviceCache.instance.getTerminalParamMap().put(0x0027, 60);
        DeviceCache.instance.getTerminalParamMap().put(0x0028, 3);
        DeviceCache.instance.getTerminalParamMap().put(0x0029, 30);

        DeviceCache.instance.getTerminalParamMap().put(0x002C, 100);
        DeviceCache.instance.getTerminalParamMap().put(0x002D, 100);
        DeviceCache.instance.getTerminalParamMap().put(0x002E, 100);
        DeviceCache.instance.getTerminalParamMap().put(0x002F, 10);

        DeviceCache.instance.getTerminalParamMap().put(0x0030, 45);
        DeviceCache.instance.getTerminalParamMap().put(0x0031, 20);
        DeviceCache.instance.getTerminalParamMap().put(0x0032, new byte[]{22, 50, 10, 30});

        DeviceCache.instance.getTerminalParamMap().put(0x0040, "18112345678");
        DeviceCache.instance.getTerminalParamMap().put(0x0041, "18111111111");
        DeviceCache.instance.getTerminalParamMap().put(0x0042, "18122222222");
        DeviceCache.instance.getTerminalParamMap().put(0x0043, "18133333333");
        DeviceCache.instance.getTerminalParamMap().put(0x0044, "18144444444");
        DeviceCache.instance.getTerminalParamMap().put(0x0045, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0046, 120);
        DeviceCache.instance.getTerminalParamMap().put(0x0047, 600);
        DeviceCache.instance.getTerminalParamMap().put(0x0048, "18155555555");
        DeviceCache.instance.getTerminalParamMap().put(0x0049, "18166666666");

        DeviceCache.instance.getTerminalParamMap().put(0x0050, 38);
        DeviceCache.instance.getTerminalParamMap().put(0x0051, 38);
        DeviceCache.instance.getTerminalParamMap().put(0x0052, 38);
        DeviceCache.instance.getTerminalParamMap().put(0x0053, 38);
        DeviceCache.instance.getTerminalParamMap().put(0x0054, 15);
        DeviceCache.instance.getTerminalParamMap().put(0x0055, 120);
        DeviceCache.instance.getTerminalParamMap().put(0x0056, 120);
        DeviceCache.instance.getTerminalParamMap().put(0x0057, 14400);
        DeviceCache.instance.getTerminalParamMap().put(0x0058, 43200);
        DeviceCache.instance.getTerminalParamMap().put(0x0059, 1200);
        DeviceCache.instance.getTerminalParamMap().put(0x005A, 3600);
        DeviceCache.instance.getTerminalParamMap().put(0x005B, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x005C, 600);
        DeviceCache.instance.getTerminalParamMap().put(0x005D, 0xA0A);
        DeviceCache.instance.getTerminalParamMap().put(0x005E, 30);

        DeviceCache.instance.getTerminalParamMap().put(0x0064, 15);
        DeviceCache.instance.getTerminalParamMap().put(0x0065, 15);

        DeviceCache.instance.getTerminalParamMap().put(0x0070, 3);
        DeviceCache.instance.getTerminalParamMap().put(0x0071, 10);
        DeviceCache.instance.getTerminalParamMap().put(0x0072, 11);
        DeviceCache.instance.getTerminalParamMap().put(0x0073, 12);
        DeviceCache.instance.getTerminalParamMap().put(0x0074, 200);

        DeviceCache.instance.getTerminalParamMap().put(0x0080, 5101);
        DeviceCache.instance.getTerminalParamMap().put(0x0081, 51);
        DeviceCache.instance.getTerminalParamMap().put(0x0082, 7);
        DeviceCache.instance.getTerminalParamMap().put(0x0083, "川A10781");
        DeviceCache.instance.getTerminalParamMap().put(0x0084, "2");

        DeviceCache.instance.getTerminalParamMap().put(0x0090, 15);
        DeviceCache.instance.getTerminalParamMap().put(0x0091, 0x03);
        DeviceCache.instance.getTerminalParamMap().put(0x0092, 0x01);
        DeviceCache.instance.getTerminalParamMap().put(0x0093, 1);
        DeviceCache.instance.getTerminalParamMap().put(0x0094, 0x00);
        DeviceCache.instance.getTerminalParamMap().put(0x0095, 0x01);

        DeviceCache.instance.getTerminalParamMap().put(0x0100, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0101, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0102, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0103, 0);
        DeviceCache.instance.getTerminalParamMap().put(0x0110, new byte[]{0, 0, 0, 0, 0, 0, 0, 0});
    }

    /**
     * 扫描所有的处理方法
     *
     * @author shuiyun
     * @date 2022-04-20 14:06
     */
    private void initEndpoint() {
        Set<Class<?>> classes = ClassUtil.scanPackage(ENDPOINT_PACKAGE_PATH);
        classes.forEach(clazz -> {
            for (Method method : clazz.getMethods()) {
                Mapping mapping = method.getAnnotation(Mapping.class);
                if (ObjectUtil.isNotNull(mapping)) {
                    Handler handler = new Handler(clazz, method, mapping.desc());
                    for (int id : mapping.types()) {
                        SessionKey.instance.getHandlerMap().put(id, handler);
                    }
                }
            }
        });
    }

    /**
     * 扫描加载所有的message
     *
     * @author shuiyun
     * @date 2022-04-20 10:20
     */
    private void initMessage() {
        Set<Class<?>> classes = ClassUtil.scanPackage(MESSAGE_PACKAGE_PATH);
        classes.forEach(clazz -> {
            Message message = clazz.getAnnotation(Message.class);
            if (ObjectUtil.isNull(message)) {
                return;
            }
            for (int id : message.value()) {
                SessionKey.instance.getMessageMap().put(id, clazz);
            }
        });
    }
}
